﻿@using MvcApplication1.Models;
@{
    ViewBag.Title = "Admin";
}
<script type="text/javascript">
    //页面URL
    var usermanageurl = '@Url.Action("Index")';
    //排序参数
    var sortorder = '@ViewBag.sortorder';
    var sortparam = 'sortorder=' + sortorder;
    //搜索参数
    $('#brandname_search').val('@ViewBag.brandname_search');
    var searchparam = 'brandname=' + '@ViewBag.brandname_search';
    var pageparam = 'page=' + '@ViewBag.pagenumber';
    //generate url incluing the parameters for searching, sorting and paging 
    function generateUrl() {
        return usermanageurl + '?' + searchparam + '&' + sortparam + '&' + pageparam;
    }
    //---------paging function----------------------------
    function firstpage() {
        pageparam = 'page=1';
        window.location = generateUrl();
    }
    function lastpage(pagecount) {
        pageparam = 'page=' + pagecount;
        window.location = generateUrl();
    }
    function prevpage(pagenumber) {
        pageparam = 'page=' + (pagenumber - 1);
        window.location = generateUrl();
    }
    function nextpage(pagenumber) {
        pageparam = 'page=' + (pagenumber + 1);
        window.location = generateUrl();
    }
    //-----------paging function-----------------------------

    //-----------sorting function----------------------------
    function sorting(element, defaultval, val1, val2) {
        var val = element.attr('value');
        if (val == '') {
            element.attr('value', defaultval);
        } else {
            if (val == val1) element.attr('value', val2);
            else element.attr('value', val1);
        }
        sortparam = 'sortorder=' + element.attr('value');
        window.location = generateUrl();
    }
    //--------sorting function-----------------------------
</script>

<h2>Admin</h2>
<div>
    <ul>
        <li>@Html.ActionLink("品牌管理","Index")</li><li>@Html.ActionLink("用户管理","UserAccountManage")</li>
    </ul>
</div>
@{
    int pagenumber = ViewBag.pagenumber;
    int pagecount = ViewBag.pagecount;
    <div><label>品牌名搜索</label><input type="text" id="brandname_search" /><button id="btn_brandname_search">搜索</button><button id="clear_search">清除搜索</button></div>
    <table border="1">
            <thead>
                <tr>
                    <th>
                        <div id="grid_brandname" value="">
                        品牌名称
                        </div>
                    </th>
                    <th>
                        <div id="grid_registertime" value="">
                        注册时间
                        </div>
                    </th>
                    <th>
                        商城地址
                    </th>
                    <th>
                        <div id="grid_itemcount" value="">
                        商品总数
                        </div>
                    </th>
                </tr>
            </thead>
            @foreach (Brand brand in ViewBag.brandlist)
            {
                <tr>
                    <td>@brand.BrandName</td>
                    <td>@brand.RegisterTime</td>
                    <td>@brand.LinkAddr</td>
                    <td>@brand.itemCount</td>
                    <td>@Html.ActionLink("详细信息", "AdminEditBrand", new { brandid = brand.ID })</td>
                    <td>@Html.ActionLink("查看商品","AdminEditItem",new {brandid=brand.ID})</td>
                </tr>    
            }
    </table>
}
@helper PageLink(int pagenumber, int pagecount)
    {
    <div id="pager">
        @if (pagenumber == 1)
        {
            <text>第一页 前一页 </text>
        }
        else
        {
            <a onclick="firstpage()">第一页</a>
            <a onclick="prevpage(@pagenumber)">前一页</a>
        }
        <span>当前第 @pagenumber 页 共 @pagecount 页</span>
        @if (pagenumber == pagecount)
        {
            <text>后一页 最后一页</text>
        }
        else
        {
            <a onclick="nextpage(@pagenumber)">后一页</a>
            <a onclick="lastpage(@pagecount)">最后一页</a>
        }
    </div>
}
<div id="gridbar">
    
    @PageLink(pagenumber, pagecount)
</div>
<script type="text/javascript">
    //executed after the document has loaded

    //-------------sorting------------------------------------
    $('#grid_brandname').click(function () {
        sorting($(this), 'brandname', 'brandname', 'brandnameDESC')
    });

    $('#grid_registertime').click(function () {
        sorting($(this), 'registertime', 'registertime', 'registertimeDESC')
    });

    $('#grid_itemcount').click(function () {
        sorting($(this), 'itemcount', 'itemcount', 'itemcountDESC')
    });
    switch (sortorder) {
        case "brandname":
            $('#grid_brandname').attr('value', 'brandname');
            break;
        case "brandnameDESC":
            $('#grid_brandname').attr('value', 'brandnameDESC');
            break;
        case "registertime":
            $('#grid_registertime').attr('value', 'registertime');
            break;
        case "registertimeDESC":
            $('#grid_registertime').attr('value', 'registertimeDESC');
            break;
        case "itemcount":
            $('#grid_itemcount').attr('value', 'itemcount');
            break;
        case "itemcountDESC":
            $('#grid_itemcount').attr('value', 'itemcountDESC');
            break;
    }
    //--------sorting------------------------------------------------

    //-------searching----------------------------------------------
    $('#btn_brandname_search').click(function () {
        var brandname = $('#brandname_search').val();
       
        searchparam = 'brandname=' + brandname;
        window.location = generateUrl();
    });

    $('#clear_search').click(function () {
        searchparam = '';
        window.location = generateUrl();
    });
</script>